package com.huage.courseplatform.application.service.impl;

import cn.dev33.satoken.stp.StpUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.huage.courseplatform.application.mapper.DictMapper;
import com.huage.courseplatform.application.mapper.StudentMapper;
import com.huage.courseplatform.application.model.dto.StudentDTO;
import com.huage.courseplatform.application.model.entity.Student;
import com.huage.courseplatform.application.model.entity.SysDict;
import com.huage.courseplatform.application.service.DictService;
import com.huage.courseplatform.application.service.StudentService;
import com.huage.courseplatform.framework.core.impl.BaseServiceImpl;
import com.huage.courseplatform.framework.utils.ResultUtils;
import com.huage.courseplatform.satoken.mapper.UserMapper;
import com.huage.courseplatform.satoken.model.entity.User;
import io.swagger.annotations.ApiOperation;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.cache.annotation.Caching;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;

import javax.annotation.Resource;
import java.util.List;

@Service
public class StudentServiceImpl extends BaseServiceImpl<StudentMapper, Student> implements StudentService {

    @Resource
    UserMapper userMapper;

    @Resource
    StudentMapper studentMapper;

    @Override
    @Caching(evict={@CacheEvict(cacheNames = "Service:StudentService:id", key = "#studentId"),
            @CacheEvict(cacheNames = "Service:StudentService:userId", key = "#userId")})
    public void unBindUserStudent(String userId, String studentId) {
        User user =  userMapper.selectById(userId);
        Assert.isTrue(!ObjectUtils.isEmpty(user),"找不到用户");
        Student student = getById(studentId);
        Assert.isTrue(!ObjectUtils.isEmpty(student),"找不到学生信息");

        List<String> userIdList = studentMapper.getUserIdListByStudentId(studentId);
        Assert.isTrue(!CollectionUtils.isEmpty(userIdList),"用户无学生绑定信息");
        Assert.isTrue(userIdList.size()!=1,"只绑定了一位用户，无法解绑");

        studentMapper.delUserStudent(userId,studentId);
    }

    @Override
    @Caching(evict={ @CacheEvict(cacheNames = "Service:StudentService:id", key = "#studentId"),
            @CacheEvict(cacheNames = "Service:StudentService:userId", key = "#userId")})
    public void bindUserStudent(String userId, String studentId) {
        User user =  userMapper.selectById(userId);
        Assert.isTrue(!ObjectUtils.isEmpty(user),"找不到用户");
        Student student = getById(studentId);
        Assert.isTrue(!ObjectUtils.isEmpty(student),"找不到学生信息");
        List<String> userIdList = studentMapper.getUserIdListByStudentId(studentId);
        if(!CollectionUtils.isEmpty(userIdList)){
            Assert.isTrue(userIdList.size()<=2,"学生已绑定两个用户，请先解除绑定");
        }
        studentMapper.addUserStudent(userId,studentId);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    @CacheEvict(cacheNames = "Service:StudentService:userId", key = "#student.id")
    public void addAndBindUser(Student student, String userId) {
        User user =  userMapper.selectById(userId);
        Assert.isTrue(!ObjectUtils.isEmpty(user),"找不到用户");
        save(student);
        studentMapper.addUserStudent(userId,student.getId());
    }


    @Transactional(rollbackFor = Exception.class)
    @Override
    @Caching(evict={ @CacheEvict(cacheNames = "Service:StudentService:id", key = "#studentId"),
            @CacheEvict(cacheNames = "Service:StudentService:userId",allEntries = true)})
    public void delStudentById(String id) {
        //关联课程数量
        int periodNum = studentMapper.getCountStudentPeriodById(id);

        Assert.isTrue(periodNum==0,"学生已经挂接"+periodNum+"个课时，无法删除");

        List<String> userIdList = studentMapper.getUserIdListByStudentId(id);
        if(!CollectionUtils.isEmpty(userIdList)){
            Assert.isTrue(userIdList.size()>=2,"学生已绑定两个用户，请先解除绑定");
        }

        studentMapper.delUserStudentByStudentId(id);
        studentMapper.deleteById(id);
    }

    @Override
    @Cacheable(cacheNames="Service:StudentService:userId",key = "#userId",unless="#result == null")
    public List<Student> getByUserId(String userId) {
        return studentMapper.getByUserId(userId);
    }


}
